package com.gitee.srurl.core.factory.handle;

import cn.hutool.core.util.StrUtil;
import com.gitee.srurl.core.annotation.Order;
import com.gitee.srurl.core.dto.request.GenerateShortUrlDTO;
import com.gitee.srurl.core.util.DateUtil;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author zxw
 * @date 2023/4/29 7:45
 */
@Component
@Order(1)
public class CheckAndSetParamsHandle implements IGenerateShortUrlHandle {
    @Override
    public boolean handle(GenerateShortUrlDTO generateShortUrlDTO) {

        String originUrl = generateShortUrlDTO.getOriginUrl();
        if (StrUtil.isBlank(originUrl)) {
            generateShortUrlDTO.setSuccess(false);
            return false;
        }
        generateShortUrlDTO.setTimes(Optional.ofNullable(generateShortUrlDTO.getTimes()).orElse(-1L));
        generateShortUrlDTO.setExpireTime(Optional.ofNullable(generateShortUrlDTO.getExpireTime())
                .orElse(DateUtil.halfYearAfter()));
        generateShortUrlDTO.setAlwaysNewUrl(Optional.ofNullable(generateShortUrlDTO.getAlwaysNewUrl()).orElse(false));

        return true;
    }
}
